﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ESAM_Assignment_3
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region Check if the database exist | This segment of codes is NOT to be modified
            CheckDatabase();
            #endregion
        }

        #region Set of methods to handle creation and deletion of database | This segment of codes is NOT to be modified
        /// <summary>
        /// Method to create the database and tables. Test data will be also be inserted into the tables as well.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGo_Click(object sender, EventArgs e)
        {
            string message = "";
            message += "Creating database.... <br>" + DBManager.CreateDatabase();
            message += "<br><br>Creating USERS table.... <br>" + DBManager.CreateUsersTable();
            message += "<br><br>Creating COMMENTS table.... <br>" + DBManager.CreateCommentsTable();
            message += "<br><br>Creating Relationships between tables.... <br>" + DBManager.CreateRelationship();

            lblStatus.Text = message;
            CheckDatabase();
        }

        /// <summary>
        /// Button event to delete the entire database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            string message = "";
            message += "Deleting database.... <br>" + DBManager.DropDatabase();

            lblStatus.Text = message;
            CheckDatabase();
        }

        /// <summary>
        /// Method to check the status of the database. If the database has not been created yet, inform the user to first create the database.
        /// Do NOT modify this method.
        /// </summary>
        private void CheckDatabase()
        {
            Button btnLogin = (Button)Master.FindControl("btnLogin");

            if (DBManager.IsDatabaseExist())
            {
                lblCreate.Text = "Database has already been created.";
                lblCreate.ForeColor = System.Drawing.Color.Blue;
                btnGo.Enabled = false;
                btnRemove.Enabled = true;
                btnLogin.Enabled = true;
            }
            else
            {
                lblCreate.Text = "Database has NOT been created yet. Please create it before proceeding further.";
                lblCreate.ForeColor = System.Drawing.Color.Red;
                btnGo.Enabled = true;
                btnRemove.Enabled = false;
                btnLogin.Enabled = false;
            }
        }
        #endregion
    }
}